//
//  NewDiary.m
//  iTimeline
//
//  Created by Mobioneer HV 02 on 1/9/13.
//  Copyright (c) 2013 Mobioneer Co., Ltd. All rights reserved.
//

#import "NewDiary.h"
#import "DiaryDB.h"
#import "DatePicker.h"
#import "AppDelegate.h"
@interface NewDiary ()<UITextViewDelegate,DatePickerDelegate>
{
    BOOL _check;
    BOOL _chk;
    DiaryDB *data;
    NSDate *date;
}
@property (weak, nonatomic) IBOutlet UITextView *txtContent;
@property (weak, nonatomic) IBOutlet UILabel *txtDate;
- (IBAction)btnDatePicker:(id)sender;
@end

@implementation NewDiary

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
-(void)config
{
    _check = YES;
    _chk = YES;
    date = [NSDate date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
   // [dateFormatter setDateStyle:NSDateFormatterLongStyle];
    [dateFormatter setDateFormat:@"cccc - dd/MM/yyyy"];
    NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"vi_VN"];
    [dateFormatter setLocale:usLocale];
    self.txtDate.text = [dateFormatter stringFromDate:date];
    self.txtContent.text = @"Enter content here...";
    self.txtContent.delegate = self;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStylePlain target:self action:@selector(Done:)];
    self.navigationItem.rightBarButtonItem = addButton;
    self.navigationItem.title = @"New Diary";
    [self config];
    // Do any additional setup after loading the view from its nib.
}
-(IBAction)Done:(id)sender
{
    [self.txtContent resignFirstResponder];
    if(_check)
    {
        _check = NO;
        AppDelegate *delegate = [[UIApplication sharedApplication]delegate];
        NSFetchRequest *request = [[NSFetchRequest alloc]init];
        NSEntityDescription *entity = [NSEntityDescription entityForName:@"DiaryDB" inManagedObjectContext:delegate.managedObjectContext];
        [request setEntity:entity];
        data = [NSEntityDescription insertNewObjectForEntityForName:@"DiaryDB" inManagedObjectContext:delegate.managedObjectContext];
        data.date = date;
        data.context = self.txtContent.text;
        NSError *error;
        if(![delegate.managedObjectContext save:&error])
        {
            NSLog(@"Save Fail!");
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Warring" message:@"Save Fail!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [alert show];
        }
        else
        {
            NSLog(@"Save Successed!");
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Notify" message:@"Save Successed!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [alert show];
        }

    }
    else
    {
        AppDelegate *delegate = [[UIApplication sharedApplication]delegate];
        //        NSFetchRequest *request = [[NSFetchRequest alloc]init];
        //        NSEntityDescription *entity = [NSEntityDescription entityForName:@"Data" inManagedObjectContext:delegate.managedObjectContext];
        //        [request setEntity:entity];
        data.date = date;
        data.context = self.txtContent.text;
        NSError *error;
        if(![delegate.managedObjectContext save:&error])
        {
            NSLog(@"Save Fail!");
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Warring" message:@"Save Fail!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [alert show];
        }
        else
        {
            NSLog(@"Save Successed!");
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Notify" message:@"Save Successed!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [alert show];
        }

    }
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)textViewDidBeginEditing:(UITextView *)textView
{
    if(_chk)
    {
        _chk = NO;
        self.txtContent.text = @"";
    }
    [UIView animateWithDuration:0.13f delay:0.1f options:UIViewAnimationCurveEaseInOut animations:^{ self.txtContent.frame = CGRectMake(12, 87, 297, 100);
        }completion:^(BOOL finished) {
    }];
   // self.txtContent.frame = CGRectMake(12, 87, 297, 100);
}
-(void)textViewDidEndEditing:(UITextView *)textView
{
//    [UIView animateWithDuration:0.3f delay:0.0f options:UIViewAnimationOptionCurveEaseInOut animations:^{ self.txtContent.frame = CGRectMake(12, 87, 297, 250);
//    }completion:^(BOOL finished) {
//    }];
    self.txtContent.frame = CGRectMake(12, 87, 297, 250);
}
- (IBAction)btnDatePicker:(id)sender {
    [self.txtContent resignFirstResponder];
    DatePicker *datePicker = [[DatePicker alloc] init];
    datePicker.delegate = self;
    [datePicker show];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.txtContent resignFirstResponder];
}
- (void)datePicker:(DatePicker *)datePicker dismissWithDone:(BOOL)done
{
    date = datePicker.picker.date;
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"cccc - dd/MM/yyyy"];
    NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"vi_VN"];
    [dateFormatter setLocale:usLocale];
    self.txtDate.text = [dateFormatter stringFromDate:date];
}
@end
